﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;


namespace TinyCinema.Data
{
    public class Room:Execution
    {
        public string ID { get; set; }
        public string Name { get; set; }
        int _DeskCount;
        public static ObservableCollection<Room> lstRoom { get; set; }
        public int DeskCount
        {
            get { return _DeskCount; }
            set
            {
                _DeskCount = value;
                OnPropertyChanged("DeskCount");
            }
        }
        public ObservableCollection<ObservableCollection<Desk>> lstDesk { get; set; }
        public Room(string _ID,bool IsInDeskInit)
        {
            DeskCount = 0;
            if (_ID == null) return;
            ID = _ID;
            PutTableIntoDataSet(Tables.Room);
            var tbl = ds.Tables[Tables.Room];
            var Row = tbl.Select(RoomTable.ID + "='" + ID + "'")[0];
            Name = Row[RoomTable.Name] as String;

            if (IsInDeskInit) return;
            lstDesk = new ObservableCollection<ObservableCollection<Desk>>();
            
            PutTableIntoDataSet(Tables.Desk);
            var tblDesk = ds.Tables[Tables.Desk];
            for (int i = 0; i < 20; ++i)
            {
                char c = (char)(i + 65);
                lstDesk.Add(new ObservableCollection<Desk>());
                var Rows = tblDesk.Select(DeskTable.RoomID + "='" + ID + "' AND " + DeskTable.Range + "='" + c + "'");
                foreach (var row in Rows)
                {
                    var Temp = new Desk(row[DeskTable.ID] as string);
                    lstDesk[i].Add(Temp);
                    if (Temp.Order != 0) ++DeskCount;                    
                }
            }     
            if (lstRoom==null)
            {
                lstRoom = new ObservableCollection<Room>();
                lstRoom = GetAllRoom();
            }
        }
        public void DeskCountRefresh()
        {
            DeskCount = 0;
            foreach (var Row in lstDesk)
                foreach (var Desk in Row)
                    if (Desk.Order!=0) ++DeskCount;
        }
        public Room() 
        {
            ID = IDGenerate(Tables.Room);
            Save();
            lstDesk = new ObservableCollection<ObservableCollection<Desk>>();
            for (int i = 0; i < 15; ++i)
            {
                char c = (char)(i+65);
                lstDesk.Add(new ObservableCollection<Desk>());
                for (int j = 1; j < 16; ++j)
                    lstDesk[i].Add(new Desk(this, c.ToString(), j));
            }
            DeskCountRefresh();
            lstRoom.Add(this);
        }
        public static ObservableCollection<Room> GetAllRoom()
        {
            var Result = new ObservableCollection<Room>();
            PutTableIntoDataSet(Tables.Room);
            var tbl = ds.Tables[Tables.Room];
            var Rows = tbl.Select();
            foreach (var Row in Rows)
                Result.Add(new Room(Row[RoomTable.ID] as string,false));
            return Result;
        }
        public void Save()
        {
            var tbl = ds.Tables[Tables.Room];
            var Rows = tbl.Select(RoomTable.ID + "='" + ID + "'");
            if (Rows.Length == 0)
            {
                var Row = tbl.NewRow();
                Row[RoomTable.ID] = ID;
                Row[RoomTable.Name] = Name;
                tbl.Rows.Add(Row);
            }
            else
            {
                var Row = Rows[0];
                Row[RoomTable.Name] = Name;
            }
            SaveCommand(Tables.Room);
        }
        public void Delete()
        {
            //Xóa xuất chiếu tại phòng chiếu
            PutTableIntoDataSet(Tables.Schedule);
            using (var tbl = ds.Tables[Tables.Schedule])
            {
                var Rows = tbl.Select(ScheduleTable.RoomID + "='" + ID + "'");
                if (Rows.Length > 0) throw new Exception();
            }

            //Xóa ghế chiếu
            PutTableIntoDataSet(Tables.Desk);
            PutTableIntoDataSet(Tables.Ticket);

            using (var tbl = ds.Tables[Tables.Ticket])
            {
                bool check = true;
                foreach (var Desks in lstDesk)
                    foreach (var Desk in Desks)
                        if (tbl.Select(TicketTable.DeskID + "='" + Desk.Room.ID + "'").Length > 0)
                            check = false;
                if(check)
                {
                    foreach (var Desks in lstDesk)
                        foreach (var Desk in Desks)
                            Desk.Delete();
                    SaveCommand(Tables.Desk);
                }
                
            }

            //Xóa phòng chiếu
            PutTableIntoDataSet(Tables.Room);
            using (var tbl = ds.Tables[Tables.Room])
            {
                var Row = tbl.Select(RoomTable.ID + "='" + ID + "'")[0];
                Row.Delete();
                SaveCommand(Tables.Room);
            }
            lstRoom = GetAllRoom();
            
        }
        public void DeskOrderRefresh()
        {
            foreach(var Row in lstDesk)
            {
                int i = 1;
                foreach (var Desk in Row)
                    if (Desk.Order != 0) Desk.Order = i++;
            }
        }

    }
}
